# 使用xpath解析网页获取视频信息
from lxml import etree
import requests
from unsplash_spider_image.user_agent_kuaidaili_proxy import get_random_ua
proxy = {'http': 'http://t10635913038651:09ht71vf@tps152.kdlapi.com:15818/', 'https': 'http://t10635913038651:09ht71vf@tps152.kdlapi.com:15818/'}

url = 'https://coverr.co/s?q=finance'
next_url = "https://coverr.co/videos/a-man-counting-money-pmm0ddf0g4"
new_user_agent = get_random_ua()
# headers = {
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
#     'Accept-Encoding': 'gzip, deflate, br',
#     'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
#     'Cache-Control': 'max-age=0',
#     'Cookie': '_ga=GA1.1.483180747.1701652296; __gads=ID=7ec83aae31d96ab2:T=1701652297:RT=1701674829:S=ALNI_MbuSXc5XwwK6GzHEV7UDwqTwWXseA; __gpi=UID=00000ca45f3e58dd:T=1701652297:RT=1701674829:S=ALNI_MYQqXHIjVktSBUbDcFh81rWDJO2AA; FCNEC=%5B%5B%22AKsRol9FZ1FEi7iXdz9t9ZEwJSbaY3CE2PB2Z-77aXrCM53AmuLSpifFjZjXtX4X5RkuMNEm-BBBMIgJq-bculTNELdF4sNkuoyiMA1xEZOyGfCHWQn_FMVtl8ouLvgZQsLlPvImlNdIAnNcgei9XRP6aeuIVbKeyg%3D%3D%22%5D%2Cnull%2C%5B%5D%5D; _ga_476D0V85QB=GS1.1.1701673650.4.1.1701676356.60.0.0',
#     'If-Modified-Since': 'Fri, 10 Nov 2023 13:59:02 GMT',
#     'If-None-Match': 'W/"9c0112e80e4e18996a753befe5b6ef2a"',
#     'Sec-Ch-Ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
#     'Sec-Ch-Ua-Mobile': '?0',
#     'Sec-Ch-Ua-Platform': '"Windows"',
#     'Sec-Fetch-Dest': 'document',
#     'Sec-Fetch-Mode': 'navigate',
#     'Sec-Fetch-Site': 'none',
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
# # }

headers = {
    'Connection': 'close',
    'Cookie': 'ab_limited_downloads=limited_downloads_daily; pseudo_user_id=djzQQoNTzYC2; _ALGOLIA=anonymous-28dcd4aa-f40e-419e-8f15-0082a9fa245f; _gid=GA1.2.309009007.1701661878; _clck=15nx007%7C2%7Cfh9%7C0%7C1433; _hjSessionUser_1749255=eyJpZCI6IjAzMmE3YmNlLTIwYmQtNTIyOC1hOThlLTkxOWQxYjE0M2EzMCIsImNyZWF0ZWQiOjE3MDE2NjE4Nzk0NjEsImV4aXN0aW5nIjp0cnVlfQ==; _hjIncludedInSessionSample_1749255=0; _ga=GA1.2.153785875.1701661878; _hjSession_1749255=eyJpZCI6Ijk0ZTVkNzMxLTlmNjMtNGVjMy04MWM3LTM5ZjA4NjFlMWU0MSIsImNyZWF0ZWQiOjE3MDE2Nzg5NzYxMDAsImluU2FtcGxlIjpmYWxzZSwic2Vzc2lvbml6ZXJCZXRhRW5hYmxlZCI6dHJ1ZX0=; _hjAbsoluteSessionInProgress=0; _clsk=bk3mqy%7C1701678977043%7C4%7C1%7Cw.clarity.ms%2Fcollect; _ga_Q6SNLJ4XVB=GS1.1.1701678975.4.1.1701679036.60.0.0',
    'User-Agent': new_user_agent
}

response = requests.get(next_url, headers=headers)
if response.status_code == 200:
    print('请求成功')
    response.encoding = 'utf-8'
    html = response.content.decode('utf-8')
    print(html)
    tree = etree.HTML(html)
else:
    print('请求失败')

# 使用XPath获取视频详情页面
# video_urls = tree.xpath('/html/body/app-root/app-shell/main/search-page/div/videos-grid/coverr-grid[2]/div/div/coverr-video/a/@href')
# print(video_urls)


#load more 动态加在A broker works with a cryptocurrency candlestick chart//https://coverr.co/api/videos?lang=en&camel_case=true&filters=is_vertical:false&page=1&page_size=18&query=finance&sort_by=date&urls=true&extends=keywords&userId=guest
#在这个写url有premium会员的需要去除，，https://cdn.coverr.co/videos/coverr-premium-brazilian-coin-spinning-on-a-black-background-1166/360p.mp4
download_video_urls = tree.xpath( '/html/body/app-root/app-shell-pure/app-video-page/section[1]/div/div[1]/acu-video-player/div/video/@src')
print(download_video_urls)
# ['/videos/coins-falling-vpnrzyjqpi', '/videos/close-up-of-a-french-coin-franc-h6m8i3vg3k', '/videos/antique-money-from-different-countries-dcjcqcwais', '/videos/portuguese-coin-spinning-on-a-black-background-bjb5wygsq0', '/videos/an-old-coin-is-spinning-on-a-black-background-7jisn8n2ok', '/videos/brazilian-coin-spinning-against-a-black-background-rtteemmw2v', '/videos/brazilian-coins-and-banknotes-anyv2aiaf5', '/videos/brazilian-banknotes-fall-in-a-heap-mlcxroqu43', '/videos/an-old-brazilian-banknote-nupx1bcrzq', '/videos/a-man-counting-money-pmm0ddf0g4', '/videos/masculine-hands-counting-euro-notes-nj7orla7jt', '/videos/a-man-counting-50-euro-banknotes-dkl9qboz3u']

# 请求download_video_url，并获取他的视频类型，保存到本地
for download_video_url in download_video_urls:
    # download_video_url = 'https://coverr.co' + download_video_url
#     print(download_video_url)
    # video_type = download_video_url.split('.')
    # print(video_type)

    response = requests.get(download_video_url, headers=headers)
    if response.status_code == 200:
        print('请求成功')
        response.encoding = 'utf-8'
        with open('video.mp4', 'wb') as f:
        # with open('video.' + video_type, 'wb') as f:
            f.write(response.content)
    else:
        print('请求失败')

















# from requests_html import HTMLSession
# session = HTMLSession()
# response = session.get('http://example.com')
# response.html.render()
# content = response.html.html

